package soul.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

//@Component
//@Lazy(false)
public class MqSender {
    private static final Logger logger = LoggerFactory.getLogger(MqSender.class);

    private static AmqpTemplate rabbitTemplate;

    @Autowired
    public void setRabbitTemplate(AmqpTemplate rabbitTemplate) {
        MqSender.rabbitTemplate = rabbitTemplate;
    }

    public static void send(TopicType type,String context) {
        String tpl = "{\"type\":${type},\"message\":${message}}";
        String sendTxt = tpl.replace("${type}", type.ordinal() + "").replace("${message}", context);
        rabbitTemplate.convertAndSend("topic_recharge", sendTxt);
        logger.info("Rabbit MQ Sender.  Context:{}", sendTxt);
    }

    public enum TopicType{
        UNKNOWN,
        //储值卡
        PREPAID_CARD,
        //红包
        RED_ENVELOPE
    }
}
